package com.peza.bikelog.common.domain.polar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.peza.bikelog.common.util.Util;

public class HrmHrLimitSummary implements Writable{

	private Util util = Util.getInstance();
	
	private List<Selection> selections;  // integer = number of samples in selection
	
	// Row 1
	private int totalTimeLimit1; // secs
	private int timeHrAboveMax1; // secs
	private int timeHrUl1_Max;
	private int timeHrLl1_Ul1;
	private int timeHrRest_Ll1;
	private int timeHrBelowRest1;

	// Row 2
	private int maxLimit1;
	private int upperLimit1;
	private int lowerLimit1;
	private int restLimit1;

	// Row 3
	private int totalTimeLimit2; // secs
	private int timeHrAboveMax2; // secs
	private int timeHrUl2_Max;
	private int timeHrLl2_Ul2;
	private int timeHrRest_Ll2;
	private int timeHrBelowRest2;

	// Row 4
	private int maxLimit2;
	private int upperLimit2;
	private int lowerLimit2;
	private int restLimit2;

	// Row 5
	private int totalTimeLimit3; // secs
	private int timeHrAboveMax3; // secs
	private int timeHrUl3_Max;
	private int timeHrLl3_Ul3;
	private int timeHrRest_Ll3;
	private int timeHrBelowRest3;

	// Row 6
	private int maxLimit3;
	private int upperLimit3;
	private int lowerLimit3;
	private int restLimit3;
	
	public HrmHrLimitSummary(){	}
	
	public HrmHrLimitSummary(String data){
		
		List<String>lines = Arrays.asList(data.split("\n"));
		Iterator<String> iter = lines.iterator();
		selections = new ArrayList<Selection>();
		while(iter.hasNext()){
		
			try {
				String row1 = iter.next();
				String row2 = iter.next();
				String row3 = iter.next();
				String row4 = iter.next();
				String row5 = iter.next();
				String row6 = iter.next();
				
				
				// Row 1
				String [] cells = row1.split("\t");
				setTotalTimeLimit1(util.convertToInt(cells[0]));
				setTimeHrAboveMax1(util.convertToInt(cells[1]));
				setTimeHrUl1_Max(util.convertToInt(cells[2]));
				setTimeHrLl1_Ul1(util.convertToInt(cells[3]));
				setTimeHrRest_Ll1(util.convertToInt(cells[4]));
				setTimeHrBelowRest1(util.convertToInt(cells[5]));
				
				// Row 2
				cells = row2.split("\t");
				setMaxLimit1(util.convertToInt(cells[0]));
				setUpperLimit1(util.convertToInt(cells[1]));
				setLowerLimit1(util.convertToInt(cells[2]));
				setRestLimit1(util.convertToInt(cells[3]));
				
				// Row 3
				cells = row3.split("\t");
				setTotalTimeLimit2(util.convertToInt(cells[0]));
				setTimeHrAboveMax2(util.convertToInt(cells[1]));
				setTimeHrUl2_Max(util.convertToInt(cells[2]));
				setTimeHrLl2_Ul2(util.convertToInt(cells[3]));
				setTimeHrRest_Ll2(util.convertToInt(cells[4]));
				setTimeHrBelowRest2(util.convertToInt(cells[5]));
				
				// Row 4
				cells = row4.split("\t");
				setMaxLimit2(util.convertToInt(cells[0]));
				setUpperLimit2(util.convertToInt(cells[1]));
				setLowerLimit2(util.convertToInt(cells[2]));
				setRestLimit2(util.convertToInt(cells[3]));
				
				// Row 5
				cells = row5.split("\t");
				setTotalTimeLimit3(util.convertToInt(cells[0]));
				setTimeHrAboveMax3(util.convertToInt(cells[1]));
				setTimeHrUl3_Max(util.convertToInt(cells[2]));
				setTimeHrLl3_Ul3(util.convertToInt(cells[3]));
				setTimeHrRest_Ll3(util.convertToInt(cells[4]));
				setTimeHrBelowRest3(util.convertToInt(cells[5]));
				
				// Row 6
				cells = row6.split("\t");
				setMaxLimit3(util.convertToInt(cells[0]));
				setUpperLimit3(util.convertToInt(cells[1]));
				setLowerLimit3(util.convertToInt(cells[2]));
				setRestLimit3(util.convertToInt(cells[3]));
				
				// Row 7-n
				
				while (iter.hasNext()){
					Selection sel = new Selection();
					String row7 = iter.next();
					cells = row7.split("\t");
					sel.setStartSample(util.convertToInt(cells[0]));
					sel.setEndSample(util.convertToInt(cells[1]));
					selections.add(sel);
				}
				
	
			} catch (Exception e) {
				System.err.println(e);
			}
			
			
			
		}

		
	}

	
	@SuppressWarnings("unused")	
	private class Selection {
		
		// Row 7 : selection
		private int startSample;
		private int endSample;
	
		public int getStartSample() {
			return startSample;
		}
		public void setStartSample(int startSample) {
			this.startSample = startSample;
		}
		public int getEndSample() {
			return endSample;
		}
		public void setEndSample(int endSample) {
			this.endSample = endSample;
		}
	}


	public List<Selection> getSelections() {
		return selections;
	}


	public void setSelections(List<Selection> selections) {
		this.selections = selections;
	}
	
	public int getTotalTimeLimit1() {
		return totalTimeLimit1;
	}
	public void setTotalTimeLimit1(int totalTimeLimit1) {
		this.totalTimeLimit1 = totalTimeLimit1;
	}
	public int getTimeHrAboveMax1() {
		return timeHrAboveMax1;
	}
	public void setTimeHrAboveMax1(int timeHrAboveMax1) {
		this.timeHrAboveMax1 = timeHrAboveMax1;
	}
	public int getTimeHrUl1_Max() {
		return timeHrUl1_Max;
	}
	public void setTimeHrUl1_Max(int timeHrUl1_Max) {
		this.timeHrUl1_Max = timeHrUl1_Max;
	}
	public int getTimeHrLl1_Ul1() {
		return timeHrLl1_Ul1;
	}
	public void setTimeHrLl1_Ul1(int timeHrLl1_Ul1) {
		this.timeHrLl1_Ul1 = timeHrLl1_Ul1;
	}
	public int getTimeHrRest_Ll1() {
		return timeHrRest_Ll1;
	}
	public void setTimeHrRest_Ll1(int timeHrRest_Ll1) {
		this.timeHrRest_Ll1 = timeHrRest_Ll1;
	}
	public int getTimeHrBelowRest1() {
		return timeHrBelowRest1;
	}
	public void setTimeHrBelowRest1(int timeHrBelowRest1) {
		this.timeHrBelowRest1 = timeHrBelowRest1;
	}
	public int getMaxLimit1() {
		return maxLimit1;
	}
	public void setMaxLimit1(int maxLimit1) {
		this.maxLimit1 = maxLimit1;
	}
	public int getUpperLimit1() {
		return upperLimit1;
	}
	public void setUpperLimit1(int upperLimit1) {
		this.upperLimit1 = upperLimit1;
	}
	public int getLowerLimit1() {
		return lowerLimit1;
	}
	public void setLowerLimit1(int lowerLimit1) {
		this.lowerLimit1 = lowerLimit1;
	}
	public int getRestLimit1() {
		return restLimit1;
	}
	public void setRestLimit1(int restLimit1) {
		this.restLimit1 = restLimit1;
	}
	public int getTotalTimeLimit2() {
		return totalTimeLimit2;
	}
	public void setTotalTimeLimit2(int totalTimeLimit2) {
		this.totalTimeLimit2 = totalTimeLimit2;
	}
	public int getTimeHrAboveMax2() {
		return timeHrAboveMax2;
	}
	public void setTimeHrAboveMax2(int timeHrAboveMax2) {
		this.timeHrAboveMax2 = timeHrAboveMax2;
	}
	public int getTimeHrUl2_Max() {
		return timeHrUl2_Max;
	}
	public void setTimeHrUl2_Max(int timeHrUl2_Max) {
		this.timeHrUl2_Max = timeHrUl2_Max;
	}
	public int getTimeHrLl2_Ul2() {
		return timeHrLl2_Ul2;
	}
	public void setTimeHrLl2_Ul2(int timeHrLl2_Ul2) {
		this.timeHrLl2_Ul2 = timeHrLl2_Ul2;
	}
	public int getTimeHrRest_Ll2() {
		return timeHrRest_Ll2;
	}
	public void setTimeHrRest_Ll2(int timeHrRest_Ll2) {
		this.timeHrRest_Ll2 = timeHrRest_Ll2;
	}
	public int getTimeHrBelowRest2() {
		return timeHrBelowRest2;
	}
	public void setTimeHrBelowRest2(int timeHrBelowRest2) {
		this.timeHrBelowRest2 = timeHrBelowRest2;
	}
	public int getMaxLimit2() {
		return maxLimit2;
	}
	public void setMaxLimit2(int maxLimit2) {
		this.maxLimit2 = maxLimit2;
	}
	public int getUpperLimit2() {
		return upperLimit2;
	}
	public void setUpperLimit2(int upperLimit2) {
		this.upperLimit2 = upperLimit2;
	}
	public int getLowerLimit2() {
		return lowerLimit2;
	}
	public void setLowerLimit2(int lowerLimit2) {
		this.lowerLimit2 = lowerLimit2;
	}
	public int getRestLimit2() {
		return restLimit2;
	}
	public void setRestLimit2(int restLimit2) {
		this.restLimit2 = restLimit2;
	}
	public int getTotalTimeLimit3() {
		return totalTimeLimit3;
	}
	public void setTotalTimeLimit3(int totalTimeLimit3) {
		this.totalTimeLimit3 = totalTimeLimit3;
	}
	public int getTimeHrAboveMax3() {
		return timeHrAboveMax3;
	}
	public void setTimeHrAboveMax3(int timeHrAboveMax3) {
		this.timeHrAboveMax3 = timeHrAboveMax3;
	}
	public int getTimeHrUl3_Max() {
		return timeHrUl3_Max;
	}
	public void setTimeHrUl3_Max(int timeHrUl3_Max) {
		this.timeHrUl3_Max = timeHrUl3_Max;
	}
	public int getTimeHrLl3_Ul3() {
		return timeHrLl3_Ul3;
	}
	public void setTimeHrLl3_Ul3(int timeHrLl3_Ul3) {
		this.timeHrLl3_Ul3 = timeHrLl3_Ul3;
	}
	public int getTimeHrRest_Ll3() {
		return timeHrRest_Ll3;
	}
	public void setTimeHrRest_Ll3(int timeHrRest_Ll3) {
		this.timeHrRest_Ll3 = timeHrRest_Ll3;
	}
	public int getTimeHrBelowRest3() {
		return timeHrBelowRest3;
	}
	public void setTimeHrBelowRest3(int timeHrBelowRest3) {
		this.timeHrBelowRest3 = timeHrBelowRest3;
	}
	public int getMaxLimit3() {
		return maxLimit3;
	}
	public void setMaxLimit3(int maxLimit3) {
		this.maxLimit3 = maxLimit3;
	}
	public int getUpperLimit3() {
		return upperLimit3;
	}
	public void setUpperLimit3(int upperLimit3) {
		this.upperLimit3 = upperLimit3;
	}
	public int getLowerLimit3() {
		return lowerLimit3;
	}
	public void setLowerLimit3(int lowerLimit3) {
		this.lowerLimit3 = lowerLimit3;
	}
	public int getRestLimit3() {
		return restLimit3;
	}
	public void setRestLimit3(int restLimit3) {
		this.restLimit3 = restLimit3;
	}

	@Override
	public String toFileString() {
		StringBuffer buffer = new StringBuffer("[Summary-123]\n");
		// Row 1
		buffer.append(totalTimeLimit1 +"\t"+ timeHrAboveMax1 +"\t"+ timeHrUl1_Max +"\t"); 
		buffer.append(timeHrLl1_Ul1 +"\t"+ timeHrRest_Ll1 +"\t"+ timeHrBelowRest1 +"\n");

		// Row 2
		buffer.append(maxLimit1 +"\t"+ upperLimit1 +"\t"+ lowerLimit1 +"\t"+ restLimit1 +"\n");

		// Row 3
		buffer.append(totalTimeLimit2 +"\t"+ timeHrAboveMax2 +"\t"+ timeHrUl2_Max +"\t");
		buffer.append(timeHrLl2_Ul2 +"\t"+ timeHrRest_Ll2 +"\t"+timeHrBelowRest2 +"\n");

		// Row 4
		buffer.append(maxLimit2 +"\t"+ upperLimit2 +"\t"+ lowerLimit2 +"\t"+ restLimit2 +"\n");

		// Row 5
		buffer.append(totalTimeLimit3 +"\t"+ timeHrAboveMax3 +"\t"+ timeHrUl3_Max +"\t");
		buffer.append(timeHrLl3_Ul3 +"\t"+ timeHrRest_Ll3 +"\t"+timeHrBelowRest3 +"\n");
		
		// Row 6
		buffer.append(maxLimit2 +"\t"+ upperLimit2 +"\t"+ lowerLimit2 +"\t"+ restLimit2 +"\n");
		
		// Row 7-n
		for (Selection sel : selections){
			buffer.append(sel.getStartSample() +"\t"+sel.getEndSample()+"\n");
		}
		
		buffer.append("\n");
		
		return buffer.toString();
	}
	

	
}
